Image Processing 1

Table of Contents

This is a “pair” assignment, which means that if you are working on a team with someone else, you and your partner should do your best to engage in the pair programming model. At any point in time, one of you is “driving,” i.e. actually using the keyboard and mouse. The other is actively engaged following along, preventing bugs, and providing ideas.

You should make sure that over the course of an assignment that you spend roughly the same amount of time each “driving.” I will also ask you to turn in a form rating the work that your partner does. My recommendation is to take turns approximately every 15 minutes or so. Set a timer to help you remember.

1. Overview

This is your chance to practice some basics of image processing. This is a warmup to the next assignment, where you’ll do some “green screen”-like special effects.

2. Get started

Create a folder in which to store your work for this assignment.

  • If you are working on your own computer, it’s up to you where to put the folder. Your desktop is likely as good a place as any. Make a folder titled image1.
  • If you are working in the labs in Olin, make sure to first mount the COURSES folder, so that you won’t lose your code when you log out. Once you’ve done so, open up Finder, then navigate to your personal student work folder. You can then make a image1 folder within there.
  • Once you’ve done so, you should then open up your new folder in VS Code. To do so, start up VS Code, then drag your folder onto the VS Code window. This should open up the folder within VS Code. If you are asked, click that you trust the authors.

3. Get setup

To facilitate reading in, storing, and manipulating image files in “standard” formats (like .jpg, .gif, and .png), we will use a module called images.py. Download and put it in your folder.

You’ll also need some images to work with. You can use pretty much any photos that you’ve taken yourselves. If you download photos, make sure that they are appropriately licensed for your use (e.g., via Creative Commons, but make sure to check with CC license is being used.). Here is one photo of Budapest I took while I was there.

4. Applying basic filters to an image

We’ll start out with some basic image manipulations first. Your task is to write a Python module called photolab1.py. It should contain the following functions:

4.1. onlyRed(image)

Creates and returns a new image containing only the red aspects of the original image. You can do this by setting all the green and blue values to 0.

4.2. negate(image)

Creates and returns a new image that is a conversion of the original image to “negative” form (like a photographic negative). Here, you don’t necessarily have to duplicate precisely the same technical visual effects that a photonegative actually uses. But you should modify all colors in a way that turns black into white, white into black, dark colors into light, light into dark, etc.

4.3. main()

Write a main() function that tests the above functions by using an image file and making red and negative versions of it appear on the screen.

win1 = ImageWin(480, 640, "Budapest")
imageOriginal = FileImage("budapest111.gif")
imageOriginal.draw(win1)

win2 = ImageWin(480, 650, "Red tinted")
redImage = onlyRed(imageOriginal)
redImage.draw(win2)

win3 = ImageWin(480, 650, "Negated")
redImage = negate(imageOriginal)
redImage.draw(win3)

5. Test your code

I have also included testing file tests_m.py, which we’ll use to test if you’re doing your code correctly. You can use pytest, as we’ve done in previous assignments, to test your code yourself.

6. Exemplary

If you complete the above successfully (and have good style in your code), you will receive nearly all the points for the assignment. If you get this far, you should feel proud of your achievements! If you want to push yourself harder and go for the exemplary grade, do the following extra challenge.

This underwater image has some fish within it, but they’re really hard to see. Write an additional function called fixUnderwater(image) that returns a new image where the fish are easier to see. We will only test this function by using it on the provided image, and looking at the fish.

7. Grading

You will receive an M for this assignment if…

  • when we run pytest tests_m.py, your program passes the tests.
  • your program is written to work in general, and not to only work for the specific tests that we have provided.

You will receive a grade of E for this assignment if you satisfy the above M requirements, and …

  • your programs have a comment at the top of each function with at least 5 words describing what the function does.
  • every one of your variable names is meaningful in some way. (Names such as thing, number, etc. are not meaningful.)
  • You have at least one other comment near what you think is the trickiest part of your code, describing how it works
  • your code demonstrates a clear sequence of actions to achieve the goal at hand, and each piece is essential. Your code does not have notably more cases or conditions than it needs to.
  • your fish are easier to see than in the original image

8. Submit your work

When finished, zip up your code and submit your work through Moodle.

Good luck, and have fun! Remember that lab assistants are available to help out if you need it, and you can attend prefect sessions as well.

Author: Dave Musicant